package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import com.mall.user.IKaptchaService;
import com.mall.user.IRegisterService;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dto.*;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @description:
 * @author: XM
 * @date: 2022/1/20 19:46
 */
@RestController
@RequestMapping("user")
public class RegisterController {


    @Reference(timeout = 3000, check = false)
    private IKaptchaService iKaptchaService;


    @Reference(timeout = 3000, check = false)
    IRegisterService iRegisterService;

    @RequestMapping("register")
    @Anoymous
    public ResponseData register(@RequestBody Map<String, String> map, HttpServletRequest request) {
        //获取用户请求信息
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String email = map.get("email");
        String captcha = map.get("captcha");

        //验证传过来的验证码
        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();
        //获取cookie中uuid
        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");

        //获取请求验证码信息
        kaptchaCodeRequest.setCode(captcha);

        kaptchaCodeRequest.setUuid(uuid);

        //调用验证验证码接口
        KaptchaCodeResponse kaptchaCodeResponse = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);
        String code = kaptchaCodeResponse.getCode();
        //如果验证码输入错误
        if (!code.equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setErrorMsg(kaptchaCodeResponse.getMsg());
        }
        //验证码输入成功继续注册

        //用 request进行封装，进行参数校验
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setEmail(email);
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setUserPwd(userPwd);
        UserRegisterResponse register = iRegisterService.register(userRegisterRequest);

        //注册成功
        if (register.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(null);
        }
        //注册失败
        return new ResponseUtil<>().setErrorMsg(register.getMsg());
    }


    @GetMapping("verify")
    @Anoymous
    public ResponseData verifyRegister(String uid, String username) {
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUserName(username);
        userVerifyRequest.setUuid(uid);
        UserVerifyResponse userVerifyResponse = iRegisterService.verifyRegister(userVerifyRequest);
            return new ResponseUtil<>().setData(userVerifyResponse);
    }
}